package pers.lls.hds.queue;


/**
 * 基于Java的基础队列实现  FIFO(先进先出)
 *
 * @author fancy
 * @time 2020/2/3 0003 19:05
 */
public class BasicQueue {

    private Object[] arr;
    private int maxSize;
    private int size;
    private int start;
    private int end;


    public BasicQueue(int num) {
        maxSize = num;
        arr = new Object[num];
        start = 0;
        end = -1;
        size = num;
    }

    public void insert(Object obj) {
        if (end == maxSize - 1) {
            end = -1;
        }
        arr[++end] = obj;
        size++;
    }

    public Object remove() {
        Object temp = arr[start++];
        if (start == maxSize) {
            start = 0;
        }
        size--;
        return temp;
    }

    public Object peek() {
        return arr[start];
    }

    public boolean isEmpty() {
        return (size == 0);
    }

    public boolean isFull() {
        return (size == maxSize);
    }

    public int size() {
        return size;
    }

    public static void main(String[] args) {

        BasicQueue basicQueue = new BasicQueue(3);
        basicQueue.insert(3);
        basicQueue.insert(4);
        basicQueue.insert(5);

        System.out.println(basicQueue.peek());

    }

}
